<template>
  <div class="work-order-container p-4">
    <!-- Search Form -->
    <el-card>
      <el-form :model="searchForm" label-width="100px" class="mb-4">
        <el-row :gutter="20">
          <el-col :span="6">
            <el-form-item label="工单编码">
              <el-input v-model="searchForm.workorderCode" placeholder="请输入工单编码"/>
            </el-form-item>
          </el-col>
          <el-col :span="6">
            <el-form-item label="工单名称">
              <el-input v-model="searchForm.workorderName" placeholder="请输入工单名称"/>
            </el-form-item>
          </el-col>
          <el-col :span="6">
            <el-form-item label="来源单据">
              <el-input v-model="searchForm.sourceCode" placeholder="请输入来源单据"/>
            </el-form-item>
          </el-col>
          <el-col :span="6">
            <el-form-item label="产品编号">
              <el-input v-model="searchForm.productCode" placeholder="请输入产品编号"/>
            </el-form-item>
          </el-col>
        </el-row>
        <el-row :gutter="20">
          <el-col :span="6">
            <el-form-item label="产品名称">
              <el-input v-model="searchForm.productName" placeholder="请输入产品名称"/>
            </el-form-item>
          </el-col>
          <el-col :span="6">
            <el-form-item label="客户编码">
              <el-input v-model="searchForm.clientCode" placeholder="请输入客户编码"/>
            </el-form-item>
          </el-col>
          <el-col :span="6">
            <el-form-item label="客户名称">
              <el-input v-model="searchForm.clientName" placeholder="请输入客户名称"/>
            </el-form-item>
          </el-col>
          <el-col :span="6">
            <el-form-item label="需求日期">
              <el-date-picker
                  v-model="searchForm.requestDate"
                  type="date"
                  value-format="YYYY-MM-DD"
                  format="YYYY-MM-DD"
                  placeholder="请选择需求日期"
                  style="width: 100%"
              />
            </el-form-item>
          </el-col>
        </el-row>
        <el-row>
          <el-col :span="24" class="flex justify-center gap-4">
            <el-button type="primary" @click="handleSearch">
              <el-icon>
                <Search/>
              </el-icon>
              搜索
            </el-button>
            <el-button @click="handleReset">
              <el-icon>
                <Refresh/>
              </el-icon>
              重置
            </el-button>
          </el-col>
        </el-row>
      </el-form>
    </el-card>
    <!-- Data Table -->
    <el-card style="margin-top: 20px">
      <el-table :data="tableData" style="width: 100%" border>
        <el-table-column prop="workorderCode" label="工单编码" width="180"/>
        <el-table-column prop="workorderName" label="工单名称" width="180"/>
        <el-table-column prop="orderSource" label="工单来源" width="120">
          <template #default="scope">
            {{ getOrderSource(scope.row.orderSource) }}
          </template>
        </el-table-column>
        <el-table-column prop="clientOrderCode" label="订单编号" width="120"/>
        <el-table-column prop="productCode" label="产品编号" width="120"/>
        <el-table-column prop="productName" label="产品名称" width="150"/>
        <el-table-column prop="productSpc" label="规格型号" width="120"/>
        <el-table-column prop="unitOfMeasure" label="单位" width="80"/>
        <el-table-column prop="quantity" label="工单数量" width="100"/>
        <el-table-column prop="quantityProduced" label="已生产数量" width="120"/>
        <el-table-column prop="clientCode" label="客户编码" width="120"/>
        <el-table-column prop="clientName" label="客户名称" width="120"/>
        <el-table-column prop="requestDate" label="需求日期" width="120"/>
        <el-table-column prop="status" label="排产状态" width="100">
          <template #default="scope">
            {{ getStatusName(scope.row.status) }}
          </template>
        </el-table-column>
        <el-table-column label="操作" fixed="right" width="180">
          <template #default="scope">
            <el-button type="primary" size="small" text @click="handleEdit(scope.row)">排产</el-button>
          </template>
        </el-table-column>
      </el-table>

      <!-- Pagination -->
      <div class="justify-between">
        <el-pagination
            v-model:current-page="currentPage"
            v-model:page-size="pageSize"
            :page-sizes="[10, 20, 30, 50]"
            layout="sizes, prev, pager, next"
            :total="total"
            @size-change="handleSizeChange"
            @current-change="handleCurrentChange"
        />
      </div>
    </el-card>
    <AddProductScheduling :visible="addDialog" :dataTable="DataTable" @close="CloseScheduling"/>
  </div>
</template>

<script setup>
import {onMounted, reactive, ref} from 'vue'
import {Refresh, Search} from '@element-plus/icons-vue'
import AddProductScheduling from "@/components/ProductionScheduling/AddDialog.vue"
import {findProductionSceduling} from "@/apis/zyj/ProductionSceduling.js"


const addDialog = ref(false)

// Search form data
const searchForm = reactive({
  workorderCode: '',
  workorderName: '',
  sourceCode: '',
  productCode: '',
  productName: '',
  clientCode: '',
  clientName: '',
  requestDate: ''
})

function initData() {
  searchForm.pageSize = pageSize.value
  searchForm.currentPage = currentPage.value
  findProductionSceduling(searchForm).then(res => {
    console.log("数据", res)
    tableData.length = 0
    Object.assign(tableData, res.records)
    total.value = res.total
    currentPage.value = res.current
    pageSize.value = res.size
  })
}

onMounted(() => {
  initData();
  console.log("数据", tableData)
})

// Table data
const tableData = reactive([])

// Pagination
const currentPage = ref(1)
const pageSize = ref(10)
const total = ref(36)
const loading = ref(false)

const CloseScheduling = () => {
  addDialog.value = false
}
const DataTable = ref();

const handleEdit = (row) => {
  addDialog.value = true
  DataTable.value = row;
}

// Methods
const handleSearch = () => {
  initData();
}

const handleReset = () => {
  Object.keys(searchForm).forEach(key => {
    searchForm[key] = ''
  })
}

const handleSizeChange = (val) => {
  pageSize.value = val
  handleSearch()
}

const handleCurrentChange = (val) => {
  currentPage.value = val
  handleSearch()
}
const getStatusName = (status) => {
  switch (status) {
    case 'FINISHED':
      return '已完成'
    case 'APPROVING':
      return '审批中'
    case 'PREPARE':
      return '草稿'
    case 'REFUSE':
      return '已拒绝'
    case 'CONFIRMED':
      return '已确认'
    default:
      return ''
  }
}

function getOrderSource(a) {
  switch (a) {
    case 'ORDER':
      return '客户订单'
    case 'STORE':
      return '库存备货'
    default:
      return '未知'
  }
}
</script>

<style scoped>
.work-order-container {
  border-radius: 4px;
}

:deep(.el-form-item__label) {
  font-weight: 500;
}

:deep(.el-table th) {
  background-color: #f5f7fa;
  color: #606266;
}

.justify-between {
  margin: 20px 0;
  display: flex;
  float: right;
}
</style>